import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.PriorityQueue;

import conversion.StringInteger;


public class KillBoss {
	
	
	/**
	 * @param args
	 */
	
	static boolean[] visited = new boolean[8];
	final static int weaponNum = 3 ;
	public static void main(String[] args) {
		BufferedReader br = getFileReader(args[0]);
		String[] damageChart = null ;
		String[] isbossHealth = null;
		
		try {
			damageChart = br.readLine().split(",");
			isbossHealth = br.readLine().split(",");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		System.out.println(leastShots(damageChart, StringInteger.convertStringArraytoIntArray(isbossHealth)));
	}

	
	private static int leastShots(String[]damageChart , int[] isbossHealth ){
		PriorityQueue<Node> pq = new PriorityQueue<Node>();
		// start with first boss
		pq.add(new Node(0, 0));
		Node top = null;
		while (pq.isEmpty() == false){
			top = pq.peek();
			pq.poll();
			//System.out.println(top.getWeapons());
			System.out.println(Integer.toBinaryString(top.getWeapons()));
			
			//Make sure we don't visit the same configuration twice.
			if (visited[top.getWeapons()])  continue ;
			visited[top.getWeapons()] = true;
			
			// A quick trick to check if we have all the weapons, meaning we defeated all the bosses.
			// We use the fact that (2^numWeapons -1) ( (1 << numWeapons)  -1) will have all the numWeapons bits set to 1. 
			if (top.getWeapons() == (1 << weaponNum) - 1 ){
				System.out.println("aa");
				break;
			}
			// visit the each boss
			for (int i = 0; i < damageChart.length; i++ ){
				//Check if we've already visited this boss, then don't bother trying him again.
				if (((top.getWeapons() >> i) & 1) == 1) continue;
				
				// Now figure out what the best amount of time that we can destroy this boss is, given the weapon we have.
				// We initialize this value to the boss's health, as that is our default (with our KiloBuster).
				int best = isbossHealth[i];
				for (int j = 0 ; j < damageChart.length; j++){
					if (i == j) continue;
					
					if ((((top.getWeapons() >> j)& 1)== 1) && (damageChart[j].charAt(i)!= '0')){
						// We have this weapon and this weapon is not 0 for current boss, so try using it to defeat this boss
						int shotsNeeded = isbossHealth[i] / (damageChart[j].charAt(i) - '0');
						if (isbossHealth[i] % (damageChart[j].charAt(i) - '0') != 0 ) shotsNeeded++;
						best = Math.min(best, shotsNeeded);
					}
				}
				System.out.println("BEST: " + Integer.toString(best));
				//Add the new node to be searched, showing that we defeated boss 1, and we used 'best' shots to defeat him.
				pq.add(new Node(top.getWeapons() | (1 << i), top.getShots() + best));
			}
		}
		
		return top.getShots();
		
	}
	
	private static BufferedReader getFileReader(String filename){
		try{
			File file = new File(filename);
	        FileInputStream fis = new FileInputStream(file);
            BufferedReader buffer_reader = new BufferedReader(new InputStreamReader(fis));
            return buffer_reader;
		}
		catch(Exception e){
			System.err.println("File " + filename + " not found");
			return null;
		}
	}
	
}
